﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EFFC.Net.Returns
{
    [Serializable]
    public class ReElement
    {
        string name;
        Type t = typeof(string);
        object value;
        /// <summary>
        /// 初始化，默认为string类型数据
        /// </summary>
        /// <param name="name"></param>
        public ReElement(string name)
        {
            this.name = name;
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="name"></param>
        public ReElement(string name, Type t)
        {
            this.name = name;
            this.t = t;
        }
        /// <summary>
        /// 参数名称
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
        /// <summary>
        ///  数据类型，默认为string类型
        /// </summary>
        public Type PType
        {
            get
            {
                return t;
            }
            set
            {
                t = value;
            }
        }
        /// <summary>
        /// 参数的值，如果赋值的value与PType不匹配则抛出异常
        /// </summary>
        public object Value
        {
            get
            {
                return value;
            }
            set
            {
                bool isType = false;
                if (value == null)
                {
                    isType = true;
                }
                else if (value.GetType().Equals(t))
                {
                    isType = true;
                }
                else if (value.GetType().BaseType.Equals(t))
                {
                    isType = true;
                }
                else
                {
                    foreach (Type tt in value.GetType().GetInterfaces())
                    {
                        if (tt.Equals(t))
                        {
                            isType = true;
                            break;
                        }
                    }
                }
                if (isType)
                {
                    this.value = value;
                }
                else
                {
                    throw new EFFC.Net.Exceptions.BizException(EFFC.Net.Common.ExceptionMessage.ParameterStd.Invalid_Type_Error, value.ToString(), t.FullName);
                }
            }
        }
    }
}
